
/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.Info.Vista;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.BCNTours.R;
import com.BCNTours.AR.Utils.ARLocationManager;
import com.BCNTours.AR.Utils.Coordinates;
import com.BCNTours.AR.Vista.POIButton;
import com.BCNTours.Maps.Vista.MapRoute;
import com.BCNTours.Model.Attraction;
import com.BCNTours.Model.InformationPoint;
import com.BCNTours.Model.Monument;
import com.BCNTours.Model.Museum;
import com.BCNTours.Vista.BCNTours;

/**
 * This class shows the info of a monument
 *
 */
public class InfoActivity extends Activity {
	
	private Attraction att;
	private Monument mon;
	private Museum mus;
	private InformationPoint infP;
	private int tipo;
	private int unit;
	
	private String linkMoreInfo;
	private String linkWeb;
	private String transport;
	
	Bundle extras;
	private Coordinates POIcoord;
	private Coordinates coordACT;
	
	private ARLocationManager myLocationManager;
	private Location myLocation;
	private String msg;
	static public ProgressDialog loading;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//setWindowOptions();
		//this.getWindow().setTitle("ShowMe Informacio");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.info_layout_2);
		
		extras = getIntent().getExtras();
		tipo = extras.getInt("tipo");
		unit = extras.getInt("unit");
		
		final TextView infoTitle = (TextView)findViewById(R.id.infoTitle);
		final TextView infoBasic = (TextView)findViewById(R.id.infoBasic);
		final ImageView infoImage = (ImageView)findViewById(R.id.infoImage);
		final TextView infoDistance = (TextView)findViewById(R.id.infoDistance);
		final TextView infoExtend = (TextView)findViewById(R.id.infoExtend);
		final TextView infoSchedule = (TextView)findViewById(R.id.infoSchedule);
		final TextView infoExtra = (TextView)findViewById(R.id.infoExtra);
		final TextView infoTelf = (TextView)findViewById(R.id.infoTelf);
		
		final TextView txtSchedule = (TextView)findViewById(R.id.Schedule);
		final TextView txtExtra = (TextView)findViewById(R.id.Extra);
		final TextView txtTelf = (TextView)findViewById(R.id.Telf);
		
		
		initializationLocation();
		
		
		String aux;
		switch(tipo){
		//Attractions
		case 0:
			att = (Attraction) extras.getSerializable("com.ShowMe.Model.Attraction");
			POIcoord = new Coordinates(new Float(att.getGpsLat()), new Float(att.getGpsLong()));
			
			infoTitle.setText(att.getName());
			infoImage.setImageResource(att.getImage2());
			aux = new String("");
			aux += att.getAddress()+"\n";
			aux += att.getCity()+" - ";
			aux += att.getCountry()+"\n";
			aux += att.getCp();
			infoBasic.setText(aux);
			infoExtend.setText(att.getInfo());
			
			linkMoreInfo = att.getLinkMoreInformation();
			linkWeb = att.getWeb();
			transport = att.getTransport();
			
			txtSchedule.setText("Horari/Horario/Schedule");
			aux = new String("");
			aux += att.getSchedule();
			infoSchedule.setText(aux);
			
			txtExtra.setText("Preu/Precio/Price");
			aux = new String("");
			aux += att.getPrice();
			infoExtra.setText(aux);
			
			txtTelf.setText("Telèfon/Teléfono/Phone");
			infoTelf.setText(att.getTelf());
						
			esconderBotones();
    		break;
		//Monuments
		case 1:
			mon = (Monument) extras.getSerializable("com.ShowMe.Model.Monument");
			POIcoord = new Coordinates(new Float(mon.getGpsLat()), new Float(mon.getGpsLong()));
			
			infoTitle.setText(mon.getName());
			infoImage.setImageResource(mon.getImage2());
			aux = new String("");
			aux += mon.getAddress()+"\n";
			aux += mon.getCity()+" - ";
			aux += mon.getCountry()+"\n";
			aux += mon.getCp()+"\n";
			infoBasic.setText(aux);
			
			aux = new String("");
			aux += mon.getAuthor();
			aux += "\n\n" + mon.getInfo();
			infoExtend.setText(aux);
			
			linkMoreInfo = mon.getLinkMoreInformation();
			linkWeb = mon.getWeb();
			transport = mon.getTransport();
			
			txtSchedule.setText("Horari/Horario/Schedule");
			aux = new String("");
			aux += mon.getSchedule();
			infoSchedule.setText(aux);
			
			txtExtra.setText("Preu/Precio/Price");
			aux = new String("");
			aux += mon.getPrice();
			infoExtra.setText(aux);
			
			esconderBotones();
			break;
		//Museums
		case 2:
			mus = (Museum) extras.getSerializable("com.ShowMe.Model.Museum");
			POIcoord = new Coordinates(new Float(mus.getGpsLat()), new Float(mus.getGpsLong()));
			
			infoTitle.setText(mus.getName());
			infoImage.setImageResource(mus.getImage2());
			aux = new String("");
			aux += mus.getAddress()+"\n";
			aux += mus.getCity()+" - ";
			aux += mus.getCountry()+"\n";
			aux += mus.getCp()+"\n";
			infoBasic.setText(aux);
			infoExtend.setText(mus.getInfo());
			
			linkMoreInfo = mus.getLinkMoreInformation();
			linkWeb = mus.getWeb();
			transport = mus.getTransport();
			
			txtSchedule.setText("Horari/Horario/Schedule");
			aux = new String("");
			aux += mus.getSchedule();
			infoSchedule.setText(aux);
			
			aux = new String("");
			aux += mus.getExtra();
			infoExtra.setText(aux);
			
			esconderBotones();
			break;
		//Information points
		case 3:
			infP = (InformationPoint) extras.getSerializable("com.ShowMe.Model.InformationPoint");
			POIcoord = new Coordinates(new Float(infP.getGpsLat()), new Float(infP.getGpsLong()));
			
			infoTitle.setText(infP.getName());
			infoImage.setImageResource(infP.getImage2());
			aux = new String("");
			aux += infP.getAddress()+"\n";
			aux += infP.getCity()+" - ";
			aux += infP.getCountry()+"\n";
			aux += infP.getCp()+"\n";
			infoBasic.setText(aux);
			infoExtend.setText(infP.getInfo());
			
			linkMoreInfo = infP.getLinkMoreInformation();
			linkWeb = infP.getWeb();
			transport = infP.getTransport();
						
			txtSchedule.setText("Horari/Horario/Schedule");
			aux = new String("");
			aux += infP.getSchedule();
			infoSchedule.setText(aux);
			
			aux = new String("");
			aux += infP.getExtra();
			infoExtra.setText(aux);
			
			txtTelf.setText("Telèfon/Teléfono/Phone");
			infoTelf.setText(infP.getTelf());
			
			esconderBotones();
			break;
		
		default:
			break;
		}
	
		
		
		String distancia = new String();
		if (myLocation != null) {
			coordACT = new Coordinates(new Float(myLocation.getLatitude()), new Float(myLocation.getLongitude()));
			POIButton mButton = new POIButton(this);
			mButton.setCoord(POIcoord);
			mButton.calculateDistance(coordACT, unit);
			distancia += "----------\n"+ (Math.rint(mButton.getDistance()*100)/100);
			
			switch (unit) {
			//The length unit is m/kilometer
			case 0:
				distancia += "km.";
				break;
			//The length unit is feet/mile
			case 1:
				distancia += "mi.";
				break;
			//The length unit is yard
			case 2:
				distancia += "yd.";
				break;
			}
			
		} else {
			distancia += "----------\nNO GPS";
		}
		
		infoDistance.setText(distancia);
	
	}
	
	
	
	private void esconderBotones(){
		final ImageButton botonWeb = (ImageButton)findViewById(R.id.btnWeb);
		final ImageButton botonMoreInfo = (ImageButton)findViewById(R.id.btnMoreInfo);
		if (linkMoreInfo.equals(" ")) {
			//INVISIBLE
			//botonMoreInfo.setVisibility(100);
			//DISABLED
			botonMoreInfo.setEnabled(false);
			botonMoreInfo.setImageResource(R.drawable.iconowikipediadisabled);
		} else {
			//VISIBLE
			//botonMoreInfo.setVisibility(0);
			//DISABLED
			botonMoreInfo.setEnabled(true);
			botonMoreInfo.setImageResource(R.drawable.iconowikipedia);
		}
		if (linkWeb.equals(" ")) {
			//INVISIBLE
			//botonWeb.setVisibility(100);
			//DISABLED
			botonWeb.setEnabled(false);
			botonWeb.setImageResource(R.drawable.iconowebdisabled);
		} else {
			//VISIBLE
			//botonWeb.setVisibility(0);
			//ENABLED
			botonWeb.setEnabled(true);
			botonWeb.setImageResource(R.drawable.iconoweb);
		}
	}
	
	
	
	private void initializationLocation(){
	    myLocationManager = new ARLocationManager(getApplicationContext());
	    //myLocationManager.requestUpdates(2000, 10, locationListener);
	    Location location = myLocationManager.getLastLocation();
	    myLocation = location;
	}
	
	
	/**
	 * HowToGo button listener method
	 */
	private void howToGo() {
		double latitude;
		double longitude;
		String nom;
		switch(tipo){
		//Attractions
		case 0:
			latitude = att.getGpsLat() * 1000000;
			longitude = att.getGpsLong() * 1000000;
			nom = att.getName();
    		break;
		//Monuments
		case 1:
			latitude = mon.getGpsLat() * 1000000;
			longitude = mon.getGpsLong() * 1000000; 
			nom = mon.getName();
			break;
		//Museums
		case 2:
			latitude = mus.getGpsLat() * 1000000;
			longitude = mus.getGpsLong() * 1000000;
			nom = mus.getName();
			break;
		//Information points
		case 3:
			latitude = infP.getGpsLat() * 1000000;
			longitude = infP.getGpsLong() * 1000000;
			nom = infP.getName();
			break;
		
		default:
			latitude = 0;
			longitude = 0;
			nom = "";
			break;
		}
		
		ConnectivityManager cm = (ConnectivityManager) getApplicationContext().
		getSystemService( Context.CONNECTIVITY_SERVICE );

		if(cm.getActiveNetworkInfo() != null){
		
			Intent intent = new Intent(getApplicationContext(), MapRoute.class);
			intent.putExtra("lat", latitude);
			intent.putExtra("long", longitude);
			intent.putExtra("nom", nom);
			startActivity(intent);
		}else{
			
			Toast.makeText(getApplicationContext(),getString(R.string.no_internet),Toast.LENGTH_LONG).show();
		}
		
		
		
	}
	
	/**
	 * LinkWeb button listener method
	 */
	private void linkWeb() {
		Uri uri = Uri.parse(linkWeb);
		openPage(uri);
	}
	
	/**
	 * LinkMoreInfo (wikipedia) button listener method
	 */
	private void linkMoreInfo() {
		Uri uri = Uri.parse(linkMoreInfo);
		openPage(uri);
	}
	
	/**
	 * Transport button listener method
	 */
	private void transport() {
		Uri uri = Uri.parse(transport);
		openPage(uri);
	}
	
	/**
	 * Open web page
	 */
	private void openPage(Uri uri) {
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
	}
	
	/**
     * BtnActionListener of InfoActivity
     */
	public void btnActionPerformed(View view){
		switch(view.getId()){
			case R.id.btnHowToGo:
				msg = getString(R.string.msgloading);
				loading = ProgressDialog.show(InfoActivity.this, "", 
		                msg, true);
				howToGo();
				break;
			case R.id.btnWeb:
				linkWeb();
				break;
			case R.id.btnMoreInfo:
				linkMoreInfo();
				break;
			case R.id.btnTransport:
				transport();
				break;
		}
	}
	
	
	
	
	
	
	
	
    
	
}